<script>
	import Modal from "@/components/Modal.vue"
	import findPerson from "./components/findPerson.vue"
	import findMe from "./components/findMe.vue"
	import tags from "./components/tags.vue"
	export default {
		components: {
			tags,
			findPerson,
			findMe,
			Modal
		},
		data() {
			return {
				currentSub: {},
				fabuTipsShow: false,
				formIsShow: true,
				readed: false,
				modalShow: false,
				tabIndex: 0,
				imageUrl: '',
				dataArr: {},
				flag: false,
				defaultImage: '/static/publish/bg.png',
				imgstr: '',
				str:'',
				share_flag: false,
				textData: {},
				myInfo: {},
			}
		},
		onShareAppMessage() {
			this.readedFn2()
			return {
				title: '发布消息找到那个他/她',
				path: '/pages/index/index?id='+this.myInfo.me_invite_code,
				imageUrl: "https://seekerxcxoss.yiyankeji.vip/Label/Share_img.png"
			}
		},
		onLoad() {
			this.list();
			this.getPublishText()
			this.getMyInfo();
			
			if(uni.getStorageSync('getIsFx')){
				if(uni.getStorageSync('getIsFx').is_fb=='1'){
					this.readed = false
				}else{
					this.readed = true
				}
			}
			
		},
		onShow() {
			var that = this
			uni.getLocation({
				geocode: true, // 是否解析地址信息（返回详细格式化地址信息）
				success: res => {
					console.log(res)
					// 获取当前位置坐标
					if (res.address != undefined) {
						that.nowCity = res.address.city;
						that.locationStatus = '定位成功'
					}
				},
				fail: res => {
					that.nowCity = '获取定位信息失败'
					console.log("获取定位信息失败");
				}
			});
		},
		methods: {
			getMyInfo() {

				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/My/getMyInfo',
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						that.myInfo = r.data.data
						that.lists_count()
					},
				})
			},
			getPublishText() {
				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/My/getGzinfo',
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						that.textData = r.data.data
						console.log("textData", that.textData)
					},
				})
			},
			readedFn() {
				this.readed = "1"
				uni.setStorageSync("readed", "1")
			},
			readedFn2() {
				this.modalShow = false
				this.readed = true
			},
			handleError(e) {
				// 设置默认图片
				this.imageUrl = this.defaultImage;
			},
			uploadImages(path) {
				var that = this
				uni.uploadFile({
					url: getApp().globalData.host + '/api/Login/uploadImages',
					filePath: path,
					name: 'image',
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: res => {
						var json = JSON.parse(res.data);
						that.imgstr += json.data.image_url + ','
					}
				})
			},
			save(sub) {
			
				if (sub.name == '') {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (sub.name.length > 6) {
					uni.showToast({
						title: '姓名最多输入6字',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (sub.age == '') {
					uni.showToast({
						title: '请输入年龄',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				// if (sub.age.indexOf(".") > -1) {
				// 	uni.showToast({
				// 		title: '请输入正确的年龄',
				// 		icon: 'none',
				// 		duration: 2000,
				// 		success() {}
				// 	})
				// 	return false;
				// }
				if (parseInt(sub.age) > 100) {
					uni.showToast({
						title: '年龄不能大于100岁',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}



				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/Resource/addMyResource',
					data: {
						resource_label_id: sub.z_label_id,
						resource_sex: sub.sex,
						resource_name: sub.name,
						resource_age: sub.age,
						resource_address: sub.seek_address,
						resource_address_two: sub.seek_address_two,
						resource_school: sub.seek_school,
						resource_school_two: sub.seek_school_two,
						resource_company: sub.seek_company,
						resource_company_two: sub.seek_company_two
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						if (r.data.code == 200) {
							uni.showToast({
								title: "提交成功",
								icon: 'none',
								duration: 2000,
								success() {

								}
							})

							that.formIsShow = false;
							setTimeout(() => {
								that.formIsShow = true;
							}, 100)

						} else {
							uni.showToast({
								title: r.data.msg,
								icon: "none"
							})
						}
					},
				})
			},
			publishConfirm(sub) {

				var that = this;

				var labelData = uni.getStorageSync('labelData')
				
				var min_money = 0
				for(var i =0;i<labelData.length;i++){
					if(labelData[i]['label_id']==sub.label_id){
						min_money = labelData[i]['min_money']
					}
				}
				
				if (sub.name == '') {
					uni.showToast({
						title: '请输入对方姓名',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (sub.name.length > 6) {
					uni.showToast({
						title: '姓名最多输入6字',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (sub.sex == '') {
					uni.showToast({
						title: '请选择对方性别',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (sub.age == '') {
					uni.showToast({
						title: '请输入对方年龄',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				
				if (parseInt(sub.age) > 100) {
					uni.showToast({
						title: '年龄不能大于100岁',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				
				if (sub.qu == '') {
					uni.showToast({
						title: '请选择所在地区',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				
				
				
				// if (sub.seek_address == '') {
				// 	uni.showToast({
				// 		title: '请输入所在地址',
				// 		icon: 'none',
				// 		duration: 2000,
				// 		success() {}
				// 	})
				// 	return false;
				// }

				if (that.tabIndex == 0) {
					if (sub.seek_phone == '') {
						uni.showToast({
							title: '请输入我的联系方式',
							icon: 'none',
							duration: 2000,
							success() {}
						})
						return false;
					}
					if (sub.seek_phone.indexOf(".") > -1 || sub.seek_phone.length != 11) {
						uni.showToast({
							title: '请输入正确的联系方式',
							icon: 'none',
							duration: 2000,
							success() {}
						})
						return false;
					}
					// if (sub.seek_content == '') {
					// 	uni.showToast({
					// 		title: '请输入信息描述',
					// 		icon: 'none',
					// 		duration: 2000,
					// 		success() {}
					// 	})
					// 	return false;
					// }
					if (sub.seek_type == '') {
						uni.showToast({
							title: '请输入我的需求',
							icon: 'none',
							duration: 2000,
							success() {}
						})
						return false;
					}
					
					
					console.log(sub.seek_money)
					
					console.log('sub.seek_money',sub.seek_money,'min_money',min_money)
					if(Number(sub.seek_money)<Number(min_money)){
						uni.showToast({
							title: '您的茶水费用不能小于'+min_money+'元哦',
							icon: 'none',
							duration: 2000,
							success() {}
						})
						return false;
					}
				}

				this.currentSub = sub
				
				that.imgstr = ''
				that.str = ''
				var str = ''
				
				if (sub.seek_img_str) {
					var arr = sub.seek_img_str.split(",");
					for (var i = 0; i < arr.length; i++) {
						that.uploadImages(arr[i]);
					}
				}
				
				
				if(that.readed==false){
					if(sub.seek_money<=0){
						that.modalShow = true
						return;
					}
				}
				
				
				uni.showLoading({
					title:'加载中请稍候'
				})
				setTimeout(function(){
					str = that.imgstr.replace(/^,+|,+$/g, '');
					that.str = str
					that.fabuTipsShow = true
					uni.hideLoading()
				},3000)
			},
			submit(sub) {
				var that = this

				

				uni.request({
					url: getApp().globalData.host + '/api/My/actionSeekInfo',
					data: {
						label_id: sub.label_id,
						z_label_id: sub.z_label_id,
						sheng:sub.sheng,
						city:sub.city,
						qu:sub.qu,
						sheng_name:sub.sheng_name,
						city_name:sub.city_name,
						qu_name:sub.qu_name,
						seek_sex: sub.sex,
						seek_name: sub.name,
						seek_age: sub.age,
						seek_address: sub.seek_address,
						seek_address_two: sub.seek_address_two,
						seek_school: sub.seek_school,
						seek_school_two: sub.seek_school_two,
						seek_company: sub.seek_company,
						seek_company_two: sub.seek_company_two,
						seek_img_str: that.str,
						seek_content: sub.seek_content,
						seek_type: sub.seek_type,
						seek_phone: sub.seek_phone,
						seek_money: sub.seek_money,
						is_tk_type: sub.is_tk_type,
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						console.log(r.data)
						if (r.data.code != 200) {
							uni.showModal({
								content: r.data.msg
							})
							return false
						}
						var orderInfo = r.data.data.info
						var seek_order_number = r.data.data.seek_order_number
						if (sub.seek_money > 0) {
							wx.requestPayment({
								timeStamp: orderInfo.timeStamp,
								nonceStr: orderInfo.nonceStr,
								package: orderInfo.package,
								signType: orderInfo.signType,
								paySign: orderInfo.paySign,
								success(res) {
									uni.showToast({
										title: '支付成功',
										icon: 'none',
										duration: 2000,
										success() {
											that.formIsShow = false;
											that.fabuTipsShow = false;
											setTimeout(() => {
												that.formIsShow =
													true;
											}, 50)
											uni.switchTab({
												url: "/pages/index/index"
											})
										}
									})
								},
							})
						} else {
							that.fabuTipsShow = false;
							uni.request({
								url: getApp().globalData.host +
									'/api/My/freeSeekInfo',
								data: {
									seek_order_number: seek_order_number
								},
								header: {
									memberid: uni.getStorageSync('member_id'),
									membertoken: uni.getStorageSync(
										'member_token'),
								},
								success: (r) => {
									that.formIsShow = false;
									setTimeout(() => {
										that.formIsShow = true;
									}, 50)
									uni.showToast({
										title: '提交成功',
										icon: 'none',
										duration: 2000,
										success() {

										}
									})
								},
							})

						}
					},
				})
			},
			confirm() {
				this.modalShow = false
				this.share_flag = true
			},
			cancel() {
				this.modalShow = false
				this.share_flag = true

			},

			list() {
				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/My/getSelectList',
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						that.dataArr = r.data.data
						if(r.data.data.carousel_list){
							if(r.data.data.carousel_list[0]){
								that.imageUrl = r.data.data.carousel_list[0].carousel_img
							}
						}
						that.flag = true
						console.log(that.dataArr)
					},
				})

			},
			tabIndexChange(index) {
				this.tabIndex = index
				this.formIsShow = false;
				setTimeout(() => {
					this.formIsShow = true;
				}, 50)
			},
			lists_count() {
				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/My/getmessagewd',
					data: {
						message_type: 1
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						if(r.data.data){
							var count = r.data.data.wd_num
							console.log(count)
							uni.setTabBarBadge({
								index: 3,
								text: String(count)
							})
						}
					},
				})
			
			},
		}
	}
</script>
<template>
	<view class="publish-container overflow-hidden" v-if="flag==true">

		<!-- <div class="mask flex" @click="readedFn2" style="justify-content: center;align-items: flex-start;padding-top: 320rpx;" v-if="readed!=='1'">
			<div class="flex-col relative" style="margin-top:100rpx">
				<image src="/static/mask/line.png" class="absolute" mode="widthFix"
					style="width:170rpx;right:-50rpx; top:-250rpx"></image>
				<image src="/static/mask/title.png" mode="widthFix" style="width:500rpx"></image>
				<image src="/static/mask/pic.png" mode="widthFix" style="width:500rpx"></image>
				<div class="center mt-50" style="display:none">
					<image src="/static/mask/confirm.png" mode="widthFix" style="width:214rpx"></image>
				</div>
			</div>
		</div> -->


		<!-- ===================发布提示=================== -->
		<Modal v-if="fabuTipsShow" :confirmText="'去发布'" :showCancel="true" cancel-text="再想想"
			@cancel="fabuTipsShow=false" @confirm="submit(currentSub)">
			<div class="center mb-50 mt-20 text-gray-a4" style="line-height: 50rpx;">
				{{textData.tjfbqgzwz}}
			</div>
		</Modal>
		<!-- ===================发布提示=================== -->

		<!-- ===================弹窗在这=================== -->
		<!-- <Modal v-if="modalShow" :showCancel="true" @cancel="cancel()" @confirm="confirm" :showShare="true">
			<div class="center mb-50 mt-20 text-gray-a4">
				请分享10个群
			</div>
		</Modal> -->
		<div class="mask flex"  style="justify-content: center;align-items: flex-start;padding-top: 320rpx;" v-if="modalShow"> <!--@click="readedFn2"-->
			<div class="flex-col relative" style="margin-top:100rpx">
				<image src="/static/mask/line.png" class="absolute" mode="widthFix"
					style="width:170rpx;right:-50rpx; top:-250rpx"></image>
				<image src="/static/mask/title.png" mode="widthFix" style="width:500rpx"></image>
				<image src="/static/mask/pic.png" mode="widthFix" style="width:500rpx"></image>
				<div class="center mt-50" style="display: none;">
					<image src="/static/mask/confirm.png" mode="widthFix" style="width:214rpx"></image>
				</div>
			</div>
		</div>
		<!-- ===================弹窗在这=================== -->

		<image :src="imageUrl" @error="handleError" mode="widthFix" class="w-full absolute" style="z-index:1;"></image>

		<div class="header flex-shirnk-0 relative z-10">
			<div class="center fz34 text-gray-16 pt-50">发布</div>
		</div>

		<div class="form h-full px-30 py-35 relative z-10">
			<div class="between">
				<div @click="tabIndexChange(0)" :class="{'tab-item-active':tabIndex===0}" class="tab-item">我找谁?
					快精准发布</div>
				<div @click="tabIndexChange(1)" :class="{'tab-item-active':tabIndex===1}" class="tab-item">谁找我？匹配资源库
				</div>
			</div>

			<findPerson v-if="formIsShow" @submit="publishConfirm" :tabIndex="tabIndex" @save="save"
				:dataArr="dataArr" />
			<!-- <findMe @submit="submit" v-else /> -->
		</div>

	</view>
</template>



<style lang="less">
	.mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 99;
	}

	page {
		height: 100%;
	}

	.header {
		padding-top: 40rpx;
		height: 440rpx;
	}

	.form {
		background-color: #fff;
		overflow: auto;
	}

	.publish-container {
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.tab-item {
		width: 335rpx;
		height: 75rpx;
		align-items: center;
		justify-content: center;
		display: flex;
		border-radius: 50rpx;
		border: 1rpx solid #AC5FF8;
		font-size: 28rpx;
		color: #AC5FF8;
	}

	.tab-item-active {
		background-color: #F877B2 !important;
		color: #fff !important;
		border-color: #F877B2 !important;
	}
</style>